package com.xczl.demo.test;

import org.apache.commons.lang3.RandomUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.xczl.demo.dao.UserDao;
import com.xczl.demo.domain.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:root-context.xml" })
public class UserDaoTestCase {

	@Autowired
	private UserDao userDao;

	/**
	 * 插入用户
	 */
	@Test
	@Transactional
	public void insert() {
		User user = new User();
		user.setLevelId(1);
		user.setName("李华");
		user.setPassword("123456");
		user.setSex(User.SEX_MALE);
		user.setUserName("user" + RandomUtils.nextInt(1500, 2000));
		userDao.insertUser(user);
	}

	/**
	 * 根据用户名查询用户
	 */
	@Test
	@Transactional
	public void findByUserName() {
		User user = userDao.findByUserName("user1");
		System.out.println(user.getName() + "\t" + user.getUserName() + "\t"
				+ user.getPassword());
	}

	/**
	 * 根据ID查询用户
	 */
	@Test
	@Transactional
	public void findById() {
		User user = userDao.findById(1);
		System.out.println(user.getName() + "\t" + user.getUserName() + "\t"
				+ user.getPassword());
	}
}
